/*
 * required librarys:
 *  - SPI (arduino core)
 *  - WiFi (arduino core)
 *  - arduinoVNC (https://github.com/Links2004/arduinoVNC)
 */

#include <Arduino.h>
#include <WiFi.h>
#include <SPI.h>

#include <TFT_eSPI.h> 
#include <FT6336U.h>

#include <VNC_ST7796S.h>
#include <VNC.h>

//触摸引脚
#define I2C_SDA 18
#define I2C_SCL 19
#define RST_N_PIN -1
#define INT_N_PIN 39

const char * vnc_ip = "192.168.31.121";
const uint16_t vnc_port = 5901;
const char * vnc_pass = "vnc_pass";

const char* ssid = "ssid";
const char* password = "password";

ST7796SVNC tft = ST7796SVNC();
arduinoVNC vnc = arduinoVNC(&tft);

//触摸屏引脚定义和数据缓存
FT6336U ft6336u(I2C_SDA, I2C_SCL, RST_N_PIN, INT_N_PIN); 
FT6336U_TouchPointType tp;

void TFTnoWifi(void) {
	tft.fillScreen(TFT_BLACK);
	tft.setCursor(0, ((tft.getHeight() / 2) - (5 * 8)));
	tft.setTextColor(TFT_RED);
	tft.setTextSize(5);
	tft.println("NO WIFI!");
	tft.setTextSize(2);
	tft.println();
}

void TFTnoVNC(void) {
	tft.fillScreen(TFT_BLACK);
	tft.setCursor(0, ((tft.getHeight() / 2) - (4 * 8)));
	tft.setTextColor(TFT_GREEN);
	tft.setTextSize(4);
	tft.println("connect VNC");
	tft.setTextSize(2);
	tft.println();
	tft.print(vnc_ip);
	tft.print(":");
	tft.println(vnc_port);
}

void setup(void) {
	//Serial.begin(115200);
	//Serial.setDebugOutput(true);

	Serial.println();
	Serial.println();

	//LCD屏幕初始化
	tft.begin(); /* TFT init */
	tft.setRotation(1);//为横屏方向
	tft.height();
	//显示屏背光
	ledcSetup(10, 5000/*freq*/, 10 /*resolution*/);
	ledcAttachPin(TFT_BL, 10);
	analogReadResolution(10);
	ledcWrite(10,300);

	//触摸屏初始化
	ft6336u.begin();/* Touch init */
	ft6336u.setRotation(Rotation_1);//为横屏方向

	tft.fillScreen(TFT_BLUE);

	// We start by connecting to a WiFi network
	Serial.println();
	Serial.println();
	Serial.print("Connecting to ");
	Serial.println(ssid);

	WiFi.begin(ssid, password);
	TFTnoWifi();
	while(WiFi.status() != WL_CONNECTED) {
	  delay(500);
	  Serial.print(".");
	}
	TFTnoVNC();

	Serial.println("");
	Serial.println("WiFi connected");
	Serial.println("IP address: ");
	Serial.println(WiFi.localIP());

	Serial.println(F("[SETUP] VNC..."));

	vnc.begin(vnc_ip, vnc_port);
	vnc.setPassword(vnc_pass); // optional
}

void loop() {
    if(WiFi.status() != WL_CONNECTED) {
        vnc.reconnect();
        TFTnoWifi();
        delay(100);
    } else {
        if(vnc.connected()) {
            int16_t touchX, touchY;
            tp = ft6336u.scan();
            touchX = tp.tp[0].x;
            touchY = tp.tp[0].y;
            bool touched = tp.touch_count;
            static unsigned long lastUpdateP;
            static unsigned long lastUpdateR;
            static uint16_t lx, ly;
            if(touched)
            {
                if((millis() - lastUpdateP) > 20) {
                    vnc.mouseEvent(touchX, touchY, 0b001);
                    lx = touchX;
                    ly = touchY;
                    lastUpdateP = millis();
                    //Serial.printf("[Touch] press: 1 X: %d Y: %d\n", touchX, touchY);
                }
                lastUpdateR = 0;
            }else{
                if((millis() - lastUpdateR) > 20) {
                    vnc.mouseEvent(lx, ly, 0b000);
                    lastUpdateR = millis();
                    //Serial.printf("[Touch] press: 0 X: %d Y: %d\n", lx, ly);
                }
                lastUpdateP = 0;
            }
        }
        vnc.loop();
        if(!vnc.connected()) {
            TFTnoVNC();
            delay(5000);
        }
    }
}
